<?php

$nom = $_POST['nom_get'];
$num_t = $_POST['nb_t'];
$num_r = $_POST['nb_r'];
$num_c = $_POST['nb_c'];
$num_comb = $_POST['nb_comb'];
$num_m = $_POST['nb_m'];
$num_d = $_POST['nb_d'];
$num_co = $_POST['nb_co'];
$num_f = $_POST['nb_f'];
$num_ta = $_POST['nb_ta'];
$num_b = $_POST['nb_b'];
$num_s = $_POST['nb_s'];
$num_gf = $_POST['nb_gf'];
$num_p=$_POST['nb_pass'];

if (isset($_POST['condition'])) {
    $condition = $_POST['condition'];
} else {
    $condition = null;
}
$res = '';

$db = db::getInstance();
$att = new DonneesModel();
$filter = array(new DFC(DonneesModel::FIELD_FORM_NAME, $nom, DFC::EXACT));
$att = $att->findByFilter($db, $filter);


foreach ($att as $x) {

    $data = unserialize($x->getData());

    for ($i = 0; $i < $x->getNbrChamp(); $i++) {

//echo ($data[$i][0]);
        switch ($data[$i][0]) {

            case "text":
                $num_t++;
                //echo $res;
                if (($data[$i][8] == "") && ($condition == null)) {
                    $cond = '';
                } else {
                    $cond = "cond_champ" . $condition;
                }
                $res = $res . '<fieldset class="grey-bg champ">
<div id="id_div_txt' . $num_t . '"class="text_champ">
<br>
<label id="label_txt' . $num_t . '" class="' . $data[$i][3] . '" cond=' . $cond . '> ' . $data[$i][2] . ' </label>
<input id="text_id' . $num_t . '" class="' . $data[$i][3] . '" type="text" cond="' . $cond . '' . '" req="' . $data[$i][6] . '" name="text_name' . $num_t . '" size="10">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_text' . $num_t . '" class="button_edit_text" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_text"> </div>
</div></fieldset>';
                break;

            case "radio":
                $num_r++;
                if (($data[$i][7] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $res = $res . '<fieldset class="grey-bg champ">
<div id="id_div_radio' . $num_r . '" class="radio_champ">
<br>
<label id="label_radio' . $num_r . '" class="' . $data[$i][3] . '" cond=' . $cond . '' . '> ' . $data[$i][2] . ' </label>
<input id="radio_id' . $num_r . '" class="' . $data[$i][3] . '" type="radio" req="' . $data[$i][5] . '" cond="' . $cond . '' . '" name="radio_name' . $num_r . '" size="10">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button class="boutton_edit_radio" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_radio"> </div>
</div>
</fieldset>';

                break;

            case "combo":
                if (($data[$i][8] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_comb++;
                $res = $res . '<fieldset class="grey-bg champ">
<div id="id_div_combo' . $num_comb . '" class="combo_champ">
<br>
<label id="label_combo' . $num_comb . '"" class="' . $data[$i][3] . '" cond=' . $cond . '' . '> ' . $data[$i][2] . ' </label>
<select id="combo_id' . $num_comb . '" class="' . $data[$i][3] . '" cond="' . $cond . '' . '" req="non" name="' . $data[$i][4] . '">';
                $c = $data[$i][6];
                for ($j = 0; $j < count($c); $j++) {
                    $res = $res . '<option>' . $c[$j] . '</option>';
                }
                $res = $res . '          
</select>
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_combo' . $num_comb . '" class="button_edit_combo" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_combo"> </div>
</div>
</fieldset>';
                break;

            case "checkbox":
                if (($data[$i][7] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_c++;
                $res = $res . '<fieldset class="grey-bg champ">
<div id="id_div_checkbox' . $num_c . '" class="checkbox_champ">
<label id="label_checkbox' . $num_c . '" class="' . $data[$i][3] . '" cond=' . $cond . '> ' . $data[$i][2] . ' </label>
<input id="checkbox_id' . $num_c . '" class="' . $data[$i][3] . '" type="checkbox" cond="' . $cond . '" req="' . $data[$i][6] . '" name="checkbox_name' . $num_c . '">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_checkbo" class="bouton_edit_checkbox" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_checkbox"> </div>
</div>
</fieldset>';
                break;

            case "mail":
                if (($data[$i][6] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_m++;
                $res.='<fieldset class="grey-bg champ">
<div id="id_div_mail' . $num_m . '" class="mail_champ">
<label id="label_mail' . $num_m . '" class="' . $data[$i][3] . '" cond=' . $cond . '>' . $data[$i][2] . '</label>
<input id="mail_id' . $num_m . '" class="' . $data[$i][3] . '" type="text" cond="' . $cond . '" req="' . $data[$i][4] . '" name="mail_name' . $num_m . '">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_mail' . $num_m . '" class="bouton_edit_mail" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_mail"> </div>
</div>
</fieldset>';

                break;

            case "date":
                if (($data[$i][6] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_d++;
                $res.='<fieldset class="grey-bg champ">
<div id="id_div_date1" class="date_champ">
<br>
<label id="label_date' . $num_d . '" class="' . $data[$i][3] . '" for="simple-calendar" cond=' . $cond . '>' . $data[$i][2] . '</label>
<input id="date_id' . $num_d . '" class="' . $data[$i][3] . '" type="text" name="date_name' . $num_d . '" cond="' . $cond . '" req="' . $data[$i][4] . '" value="">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_date' . $num_d . '"class="button_edit_date" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_date"> </div>
</div>
</fieldset>';
                break;
            case "cond":
                if (($data[$i][8] == "") && ($condition == null)) {

                    $cond = '';
                } else {
                    $cond = "cond_champ" . $condition;
                }

                $num_co++;
                $res.='<fieldset class="grey-bg champ">
<div id="id_div_cond' . $num_co . '" class="champ_cond">
<label id="label_cond' . $num_co . '" class="' . $data[$i][3] . '" num="' . $num_co . '" cond="' . $cond . '"> ' . $data[$i][2] . '</label>
<input id="cond_id' . $num_co . '" class="' . $data[$i][3] . '" type="checkbox" num="' . $num_co . '" cond="' . $cond . '" req="non" name="cond_name' . $num_co . '">
<button id="reduire" class="reduire_menu_cond" type="button">-</button>
<button id="boutton_edit_cond" class="bouton_edit_cond" type="button">Editer</button>
<button class="Ajout_cond grey" type="button">Activer ajout</button>
<button class="desactiver_cond grey" type="button">desactiver ajout</button>
<button id="supprimer" class="red">X</button>
<br>
<div class="edit_show_cond' . $num_co . '"> </div>
<div id="conditions" class=""></div>
</div>
</fieldset>';
                $condition++;
                break;


            case "form":
                if (($data[$i][5] == "") && ($condition == null)) {

                    $cond = '';
                } else {
                    $cond = "cond_champ" . $condition;
                }
                
                $num_gf++;

                $res.='<fieldset class="grey-bg champ">
<div id="id_div_get_form'.$num_gf.'" class="get_form_champ">
<br>
<label id="label_get_form'.$num_gf.'" class="voir" cond="'.$cond.'"> Ajouter formulaire </label>
    <select id="invisivle" style="visibility:hidden">
    </select>
  <select id="filtret'.$num_gf.'" class="filtre" cond="'.$cond.'">';
                                
                                  $c = $data[$i][6];

            for ($j = 0; $j < count($c); $j++) {
              $res.='<option>' . $c[$j] . '</option>';
            }
                              $res.=' </select>
<button id="supprimer" class="red">X</button>
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button class="bouton_edit_form" type="button">Editer</button>
<br>
<div id="edit_show_get_form"> </div>
</div>
</fieldset>';
                break;

            case "area":
                $num_ta++;
                if (($data[$i][8] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $res.='
<fieldset class="grey-bg champ">
<div id="id_div_txtarea' . $num_ta . '" class="area_champ">
<br>
<label id="label_area' . $num_ta . '" class="' . $data[$i][3] . '" cond="' . $cond . '">' . $data[$i][2] . '</label>
<textarea id="area_id' . $num_ta . '" class="' . $data[$i][3] . '" cond="' . $cond . '" req="' . $data[$i][6] . '" rows="2" cols="30" name="area_name' . $num_ta . '"></textarea>
<br>
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button  class="button_edit_area" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_area"> </div>
</div>
</fieldset>
</div>';
                break;

            case "break":
                if (($data[$i][5] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_b++;
                $res.='
<fieldset class="grey-bg champ">
<div id="id_div_break' . $num_b . '" class="break_champ">
<br>
<label id="label_break' . $num_b . '" class="' . $data[$i][3] . '" cond="' . $cond . '"> ' . $data[$i][2] . ' </label>
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button  class="button_edit_break" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_break"></div>
</div>
</fieldset>
</div>';
                break;


            case "select":

                if (($data[$i][6] == "") && ($condition == null)) {
                    $cond = '';
                }
                else
                    $cond = "cond_champ" . $condition;
                $num_s++;
                $res.='<fieldset class="grey-bg champ">
<div id="id_div_id_select' . $num_s . '" class="id_select_champ">
<br>
<label id="label_id_select' . $num_s . '" class="' . $data[$i][3] . '" cond="' . $cond . '">'.$data[$i][2].'</label>
<select disabled="disabled" id="table_menu' . $num_s . '" class="' . $data[$i][3] . '" style="display:inline;" cond="' . $cond . '">
<option>' . $data[$i][3] . '</option></select>
<select disabled="disabled" id="colone_menu' . $num_s . '" class="colone_select voir" cond="' . $cond . '"><option>' . $data[$i][4] . ' </select>
<button id="supprimer" class="red">X</button>
</fieldset></div>';
                break;
                
                
                
                case "pass":
                $num_p++;
                
                if (($data[$i][7] == "") && ($condition == null)) {
                    $cond = '';
                } else {
                    $cond = "cond_champ" . $condition;
                }
                $res = $res . '<fieldset class="grey-bg champ">
<div id="id_div_pass'.$num_p.'" class="pass_champ">
<br>
<br>
<label id="label_pass'.$num_p.'" class="' . $data[$i][3] . '">'.$data[$i][2].'</label>
<input id="pass_id1" class="' . $data[$i][3] . '" type="password" cond="' . $cond . '" req='.$data[$i][5].' name="pass_name'.$num_p.'" size="' . $data[$i][4] . '">
<button id="reduire" class="reduire_menu_edit" type="button">-</button>
<button id="boutton_edit_pass'.$num_p.'" class="button_edit_pass" type="button">Editer</button>
<button id="supprimer" class="red">X</button>
<br>
<div id="edit_show_pass"></div>
</div>
</fieldset>';
                break;
        }
    }
}
$resultat = array('textnbr' => $num_t, 'radionbr' => $num_r, 'res' => $res,'get_form'=>$num_gf, 'checknbr' => $num_c, 'combonbr' => $num_comb, 'datenbr' => $num_d, 'mailnbr' => $num_m, 'condnbr' => $num_co, 'areanbr' => $num_ta, 'break' => $num_b, 'select' => $num_s,'pass'=>$num_p);

echo json_encode($resultat);
?>